﻿using System;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bookvar.UnitTests.ObjectModel.Actions
{
    /// <summary>
    /// Summary description for DeleteTopicActionTests
    /// </summary>
    [TestClass]
    public class DeleteTopicActionTests : CommonActionTests
    {
        /// <summary>
        /// Creates a <see cref="DeleteTopicAction"/> to be tested with the common action tests.
        /// </summary>
        /// <returns></returns>
        protected override IAction CreateAction()
        {
            var parent = new Topic();
            var topic = new Topic();
            parent.SubTopics.Add( topic );

            return new DeleteTopicAction( topic, new MindMap() );
        }

        [TestMethod]
        public void CanCreateNew()
        {
            var action = new DeleteTopicAction();
            Assert.IsNotNull( action );
        }

        [TestMethod]
        [ExpectedException( typeof ( ArgumentNullException ) )]
        public void New_WithNullTopic_Throws()
        {
            new DeleteTopicAction( null, new MindMap() );
        }

        [TestMethod]
        [ExpectedException( typeof ( ArgumentNullException ) )]
        public void New_WithNullMindMap_Throws()
        {
            new DeleteTopicAction( new Topic(), null );
        }

        [TestMethod]
        [ExpectedException( typeof ( ArgumentNullException ) )]
        public void MindMapProperty_SetToNull_Throws()
        {
            new DeleteTopicAction { Mindmap = null };
        }

        [TestMethod]
        [ExpectedException( typeof ( ArgumentNullException ) )]
        public void TopicProperty_SetToNull_Throws()
        {
            new DeleteTopicAction { Topic = null };
        }

        [TestMethod]
        [ExpectedException( typeof ( InvalidOperationException ) )]
        public void Execute_WithNotSetTopic_Throws()
        {
            new DeleteTopicAction { Mindmap = new MindMap() }.Execute();
        }

        [TestMethod]
        [ExpectedException( typeof ( InvalidOperationException ) )]
        public void Execute_WithNotSetMindMap_Throws()
        {
            new DeleteTopicAction { Topic = new Topic() }.Execute();
        }

        [TestMethod]
        public void Execute_WithConnectedTopic_RemovesTheTopicFromTheParentSubtopics()
        {
            var parent = new Topic();
            var topic = new Topic();
            parent.SubTopics.Add( topic );
            CollectionAssert.Contains( parent.SubTopics, topic );

            new DeleteTopicAction( topic, new MindMap() ).Execute();

            CollectionAssert.DoesNotContain( parent.SubTopics, topic );
        }

        [TestMethod]
        public void Undo_WithConnectedTopic_AddsBackTheTopicToTheParentSubtopics()
        {
            var parent = new Topic();
            var topic = new Topic();
            parent.SubTopics.Add( topic );
            CollectionAssert.Contains( parent.SubTopics, topic );

            var action = new DeleteTopicAction( topic, new MindMap() );
            action.Execute();
            action.Undo();

            CollectionAssert.Contains( parent.SubTopics, topic );
        }

        [TestMethod]
        public void Execute_WithFloatingTopic_RemovesTheFloatingTopicFromTheMindMap()
        {
            var topic = new Topic();
            var mindMap = new MindMap();
            mindMap.FloatingTopics.Add( topic );

            new DeleteTopicAction( topic, mindMap ).Execute();

            CollectionAssert.DoesNotContain( mindMap.FloatingTopics, topic );
        }

        [TestMethod]
        public void Undo_WithFloatingTopic_AddsBackTheTopicToTheMindMapSubtopics()
        {
            var topic = new Topic();
            var mindMap = new MindMap();
            mindMap.FloatingTopics.Add( topic );

            var action = new DeleteTopicAction( topic, mindMap );
            action.Execute();
            action.Undo();

            CollectionAssert.Contains( mindMap.FloatingTopics, topic );
        }

        [TestMethod]
        public void Execute_RemovesTheTopicPosition()
        {
            var topic = new Topic();
            var mindMap = new MindMap();
            mindMap.FloatingTopics.Add( topic );
            var position = new TopicPosition( topic.ID );
            mindMap.TopicPositions.Add( position );

            new DeleteTopicAction( topic, mindMap ).Execute();

            CollectionAssert.DoesNotContain( mindMap.TopicPositions, position );
        }

        [TestMethod]
        public void Undo_AddsBackTheTopicPosition()
        {
            var topic = new Topic();
            var mindMap = new MindMap();
            mindMap.FloatingTopics.Add( topic );
            var position = new TopicPosition( topic.ID );
            mindMap.TopicPositions.Add( position );

            var action = new DeleteTopicAction( topic, mindMap );
            action.Execute();
            action.Undo();

            CollectionAssert.Contains( mindMap.TopicPositions, position );
        }
    }
}